<!DOCTYPE html>
<html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link rel="stylesheet" href="skin/base.css">
        <link rel="stylesheet" href="skin/form.css">
        <link rel="stylesheet" href="skin/btn.css">
		<script src="js/config.js"></script>
		<script src="js/common.js"></script>
        <script src="js/zy_control.js"></script>
        <script src="js/zy_click.js"></script>
		<script src="js/login.js"></script>
    </head>
    <body ontouchstart>
        <div id="main" class="box-ver box">
			<div id="header">
				<header>
					<div class="lb" ontouchstart="zy_touch('down')" onclick="uexWindow.close(-1);"></div>
					<center>登录</center>
				</header>
			</div>
			<div id="content">
        		<div class="form">
					<section>
						<menu>
							<strong>账号：</strong>
							<label><input type="text" id="username" name="username" placeholder="请输入账号/邮箱" /></label>
						</menu>
						<menu>
							<strong>密码：</strong>
							<label><input type="password" name="password" id="password" placeholder="请输入登录密码" /></label>
						</menu>
					</section>
					
				<div class="button">
					<button class="cupid-green" type="button" ontouchstart="zy_touch('down')" onclick="login('login');">登录</button>
				</div>

				<ul class="bver">
                <li>
				<div class="button">
					<button class="shiny-blue" type="button" ontouchstart="zy_touch('down')" onclick="login('oschina');">OAuth2登录</button>
				</div>
				</li>
                <li>
				<div class="button" >
					<button class="clean-gray" type="button" ontouchstart="zy_touch('down')" >新浪微博登录</button>
				</div>
                </li>
				</ul-->		
				</div>
			</div>
		</div>
    </body>
    <script>
    	zy_init();
		var Users = new Users({});
		var type = '';
		function ok(){
			getMyInfo();
		}
		
		function getMyInfo(){
			var opId = 999;
			var url = OpenAPI.my_information+'/?client_id='+OpenAPI.client_id+'&access_token='+localStorage['access_token'];
			uexXmlHttpMgr.open(999,'get',url,30000);
			uexXmlHttpMgr.send(999);
		}
		
		function login(str){
			type=str;
			var cburl = OpenAPI.osc_redirect_uri;
			if (str == "login") {
				var username = $$('username').value;
				var password = $$('password').value;
				if (password.length < 1) {
					uexWindow.alert('', '请填写完整的账号', '关闭');
					return false;
				}
				if (password.length < 1) {
					uexWindow.alert('', '请填写密码，', '关闭');
					return false;
				}
				uexWindow.toast(1, 5, '提交中...', 0);
				var url = OpenAPI.access_token +'/?client_id='+OpenAPI.client_id+'&client_secret='+OpenAPI.client_secret+'&grant_type=password&username='+username+'&password='+password+'&dataType=json';
				uexXmlHttpMgr.open(99, 'post', url, "8000");
				uexXmlHttpMgr.send(99);
				
			}else if(str=="sina"){
				var wkey='';
				url = 'https://api.weibo.com/oauth2/authorize?client_id='+wkey+'&response_type=token&redirect_uri='+encodeURIComponent(cburl)+'&display=mobile&forcelogin=true';
				var e = $$("header");
				var y = e.offsetHeight;
				var s = window.getComputedStyle($$("content"),null);
				uexWindow.onOAuthInfo = onOAuth;
				uexWindow.openPopover('content','0',url,'',0,int(y),int(s.width),int(s.height),int(s.fontSize),'1');
			}else if(str == "oschina"){
				var url=OpenAPI.authorize+'/?client_id='+OpenAPI.osc_client_id+'&response_type=code&state=xyz&redirect_uri='+encodeURIComponent(cburl);
				var e = $$("header");
				var y = e.offsetHeight;
				var s = window.getComputedStyle($$("content"),null);
				uexWindow.onOAuthInfo = onOAuth;
				uexWindow.openPopover('content','0',url,'',0,int(y),int(s.width),int(s.height),int(s.fontSize),'1');
			}
		}

		function onOAuth(winName, url){
			if(url.indexOf("?code") > 0){
				var url_params = urlParse(url);
				uexWindow.toast(1, 5, '登录中...', 0);
				var urls = OpenAPI.access_token+'/?client_id='+OpenAPI.osc_client_id+'&client_secret='+OpenAPI.osc_client_secret+'&grant_type=authorization_code&code='+url_params['code']+'&redirect_uri='+encodeURIComponent(OpenAPI.osc_redirect_uri);
				uexXmlHttpMgr.open(98,'get',urls,"8000");
				uexXmlHttpMgr.send(98);
			}
		}
				
		window.uexOnload = function(type){
			if (!type) {
				uexXmlHttpMgr.onData = function(opId, status, data){
					uexXmlHttpMgr.close(opId);
					uexWindow.closeToast();
					if (status == 1) {
						data = JSON.parse(data);
						if(opId == 999){
							if (data['user']) {
								localStorage['uid'] = data['user'];
								localStorage['uname'] = data['name'];
								uexWindow.toast("0","5","帐号UID获取成功",1000);
							}else{
								uexWindow.toast("0","5","帐号UID获取失败",1000);
							}
							var win=localStorage['login'];
							uexWindow.evaluatePopoverScript(win,'content','oklogin("登录成功~")');	
							uexWindow.close(-1);
						}else if(opId == 99) {
							if (data['access_token']) {
								Users.logout();
								localStorage['access_token'] = data['access_token'];
								ok();
							}else{
								uexWindow.toast("0","5","登录失败~!",1000);
							}
						}else if(opId == 98){
							uexWindow.closePopover('content');
							if (data['access_token']) {
								Users.logout();
								localStorage['access_token'] = data['access_token'];
								ok();
							}else{
								uexWindow.toast("0","5","登录失败~!",1000);
							}
						}else{
								uexWindow.toast("0","5","登录失败~",1000);
							}
					}else if(status==-1){
						if (opId == 1) {
							uexWindow.closePopover('content');
						}						
						uexWindow.toast("0","5","登录超时~",1000);
					}
				}
			}
		}
    </script>
	
</html>